package com.czy.owner.ui.order;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.czy.owner.R;
import com.czy.owner.adapter.CommonTabLayoutAdapter;
import com.czy.owner.api.LogisticsApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.LogisticsModel;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.BaseFragment;
import com.czy.owner.utils.PhoneUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangyang on 2017/6/13 0013.
 * 物流信息
 */

public class LogisticsActivity extends BaseActivity {
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    @BindView(R.id.linear_empty)
    LinearLayout linearEmpty;
    private CommonTabLayoutAdapter mFragmentAdapter;
    private List<BaseFragment> mFragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private int userOrderId;

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_tablayout;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(getString(R.string.order_logistics_detail));
        userOrderId = getIntent().getIntExtra("userOrderId", 0);
        getData();
    }


    @Override
    protected void updateViews() {

    }

    private void getData() {
        LogisticsApi logisticsApi = new LogisticsApi(new HttpOnNextListener<List<LogisticsModel>>() {
            @Override
            public void onNext(List<LogisticsModel> list) {
                if (list.size() > 4) {
                    mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                } else {
                    mTabLayout.setTabMode(TabLayout.MODE_FIXED);
                }
                if (list.size() > 0 && list.size() == 1) {
                    for (int i = 0; i < list.size(); i++) {
                        mTabLayout.setVisibility(View.GONE);
                        mTabLayout.addTab(mTabLayout.newTab().setText("包裹" + (i + 1)));
                        titles.add("包裹" + (i + 1));
                        mFragments.add(LogisticsFragment.myFragment(list.get(i)));
                    }
                } else if (list.size() > 1) {
                    for (int i = 0; i < list.size(); i++) {
                        mTabLayout.addTab(mTabLayout.newTab().setText("包裹" + (i + 1)));
                        titles.add("包裹" + (i + 1));
                        mFragments.add(LogisticsFragment.myFragment(list.get(i)));
                    }
                } else {
                    PhoneUtils.ShowToastMessage(LogisticsActivity.this, "暂无物流信息");
                    mTabLayout.setVisibility(View.GONE);
                    linearEmpty.setVisibility(View.VISIBLE);
                }

                mFragmentAdapter = new CommonTabLayoutAdapter(getSupportFragmentManager(), mFragments, titles);
                viewPager.setAdapter(mFragmentAdapter);
                viewPager.setOffscreenPageLimit(list.size());
                mTabLayout.setupWithViewPager(viewPager);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        }, this);
        logisticsApi.setSession(UserHelper.getInstance().getSessionInfoModel(LogisticsActivity.this).getSession());
        logisticsApi.setUserOrderId(userOrderId + "");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(logisticsApi);
    }

}
